﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL.EstRec;
using System.Security.Principal;

namespace BAL.EstRec.AUTH
{
    public class Auth
    {

        public static string GetTerritorio() {
            
            try
            {
                DataTable dt = SrvDB.ExecuteQuery("EXEC SP_ER_GET_FDV_INFO");

                return dt.Rows[0]["PositionCode"].ToString().Trim();
            }
            catch (Exception Ex) {

                Console.Write(Ex.Message);
                return "";
            }            
        }

        public static string ActualUserTypeControl(System.Security.Principal.WindowsIdentity wi)
        {

            try
            {
                foreach (IdentityReference identRef in wi.Groups)
                {
                    IdentityReference account = identRef.Translate(typeof(NTAccount));
                    //System.Windows.MessageBox.Show(account.Value);

                    if (account.Value.ToString().ToLower().Equals(System.Configuration.ConfigurationManager.AppSettings["HQ_Group"].ToString().ToLower()))
                    {
                        return "HQ";
                    }
                    else if (account.Value.ToString().ToLower().Equals(System.Configuration.ConfigurationManager.AppSettings["FDV_Group"].ToString().ToLower()))
                    {
                        return "FDV";
                    }
                    else if (account.Value.ToString().ToLower().Equals(System.Configuration.ConfigurationManager.AppSettings["Admin_Group"].ToString().ToLower()))
                    {

                        return "Admin";
                    }
                }

                return "";
            }
            catch (Exception Ex)
            {

                Console.Write(Ex.Message);
                return "";
            }
        }
        
        public static string ActualUserType() {

            try
            {
                WindowsIdentity wi = WindowsIdentity.GetCurrent();

                AppConfs ac = new AppConfs();
                

                foreach (IdentityReference identRef in wi.Groups)
                {
                    IdentityReference account = identRef.Translate(typeof (NTAccount));
                    //System.Windows.MessageBox.Show(account.Value);

                    if (account.Value.ToString().ToLower().Equals (ac.cnf.Admin_Group.ToString().ToLower()))
                    {
                        return "Admin";
                    }
                    else if (account.Value.ToString().ToLower().Equals(ac.cnf.HQ_Group.ToString().ToLower()))
                    {
                        return "HQ";
                    }
                    else if (account.Value.ToString().ToLower().Equals(ac.cnf.FDV_Group.ToString().ToLower()))
                    {
                        return "FDV";
                    }
                }

                return "";
            }
            catch (Exception Ex) {

                Console.Write(Ex.Message);
                return "";
            }
        }
    }
}
